package com.machine.filling.function.set.orderList;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.machine.filling.R;
import com.machine.filling.base.baseMvp.BaseFragment;
import com.machine.filling.db.dao.ExceptionOrderDao;
import com.machine.filling.db.modle.ExceptionOrderModle;
import com.machine.filling.db.modle.TempOrderModle;
import com.machine.filling.function.init.INextFragment;
import com.machine.filling.function.set.SetFragment;
import com.machine.filling.function.set.excOrderDetail.ExcOrderDetailFragment;
import com.machine.filling.function.set.orderDetail.OrderDetailFragment;
import com.machine.filling.ui.view.MyLinearLayout;
import com.machine.filling.util.Common;
import com.machine.filling.util.DateUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Administrator on 2018/2/2 0002.
 *
 * @description 订单列表
 */
public class OrderListFragment extends BaseFragment<OrderListContract.Presenter> implements OrderListContract.View, OrderListAdapter.IOnItemClickListener, View.OnClickListener, RadioGroup.OnCheckedChangeListener, ExcOrderListAdapter.IOnItemClickListener {

    private INextFragment INextFragment;
    private RecyclerView recyclerview;
    private Button bt_back;
    private RadioGroup rg_time;
    private RadioGroup rg_state;
    private TextView tv_start_time;
    private TextView tv_end_time;
    private Button bt_search;

    private List<TempOrderModle> datas;

    private String isOrdered;//  1预约加油  0正常加油  2全部
    long startTimes;//查询起始时间戳
    long endTimes;//查询结束时间戳
    private OrderListAdapter orderListAdapter;
    private CalendarDialog startDialog;
    private CalendarDialog endDialog;
    private LinearLayout ll_calendar;

    private List<ExceptionOrderModle> excDatas;
    private ExcOrderListAdapter excOrderListAdapter;
    private TextView tv_total_page;
    private EditText et_now_page;
    private Button bt_first;
    private Button bt_last;
    private Button bt_prepage;
    private Button bt_nextpage;
    private Button bt_sure;
    private int totalPageCount;

    private static int pageCount = 1;//第几页
    private static final int contentCount = 20;//一页有多少数据

    /**
     * @param mIntent
     */
    public OrderListFragment(Intent mIntent) {
        super(mIntent);
    }

    @Override
    public View initViews() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.fg_orderlist, null);
        bt_first = view.findViewById(R.id.bt_first);
        bt_prepage = view.findViewById(R.id.bt_prepage);
        bt_last = view.findViewById(R.id.bt_last);
        bt_nextpage = view.findViewById(R.id.bt_nextpage);
        bt_sure = view.findViewById(R.id.bt_sure);
        tv_total_page = view.findViewById(R.id.tv_total_page);
        et_now_page = view.findViewById(R.id.et_now_page);
        rg_time = view.findViewById(R.id.rg_time);
        rg_state = view.findViewById(R.id.rb_state);
        tv_start_time = view.findViewById(R.id.tv_start_time);
        tv_end_time = view.findViewById(R.id.tv_end_time);
        bt_search = view.findViewById(R.id.bt_search);
        recyclerview = view.findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new MyLinearLayout(mContext, LinearLayoutManager.VERTICAL, false));
        ll_calendar = view.findViewById(R.id.ll_calendar);
        bt_back = view.findViewById(R.id.bt_back);
        tv_start_time.setOnClickListener(this);
        tv_end_time.setOnClickListener(this);
        bt_search.setOnClickListener(this);
        bt_back.setOnClickListener(this);
        bt_prepage.setOnClickListener(this);
        bt_nextpage.setOnClickListener(this);
        bt_first.setOnClickListener(this);
        bt_last.setOnClickListener(this);
        bt_sure.setOnClickListener(this);
        rg_state.setOnCheckedChangeListener(this);
        rg_time.setOnCheckedChangeListener(this);
        ll_calendar.setVisibility(View.GONE);
        return view;
    }

    @Override
    public void initDatas() {

        isOrdered = Common.ORDER_TYPE_ALL;
        startTimes = DateUtils.getDayFirstSecond(Calendar.getInstance());
        endTimes = DateUtils.getDayLastSecond(Calendar.getInstance());
        datas = new ArrayList<>();
        pageCount = 1;
        excDatas = new ArrayList<>();
        int totalCount = ExceptionOrderDao.getInstance().queryAllDesc(startTimes, endTimes, isOrdered).size();
        totalPageCount = (totalCount / contentCount) + (totalCount % contentCount == 0 ? 0 : 1);
        tv_total_page.setText(totalPageCount == 0 ? "1" : totalPageCount + "");
        et_now_page.setText(pageCount + "");

        excDatas = ExceptionOrderDao.getInstance()
                .queryAllDescWithPage(startTimes, endTimes, isOrdered, pageCount, contentCount);
        excOrderListAdapter = new ExcOrderListAdapter(mContext, excDatas);
        excOrderListAdapter.setOnItemClickListener(this);
        recyclerview.setAdapter(excOrderListAdapter);


    }

    public void setINextFragment(INextFragment INextFragment) {
        this.INextFragment = INextFragment;
    }

    @Override
    public void onClickItem(int position, TempOrderModle bean) {
        Intent intent = new Intent();
        intent.putExtra("bean", bean);
        INextFragment.replaceNext(intent, OrderDetailFragment.class.getName());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_start_time:
                showStartDialog();
                break;
            case R.id.tv_end_time:
                showEndDialog();
                break;
            case R.id.bt_search:
                pageCount = 1;
                getOrderListDatas();
                break;
            case R.id.bt_back:
                INextFragment.replaceNext(new Intent(), SetFragment.class.getName());
                break;
            case R.id.bt_prepage:
                if (pageCount > 1) {
                    et_now_page.setText(--pageCount + "");
                }
                getOrderListDatas();
                break;
            case R.id.bt_nextpage:
                if (pageCount < totalPageCount) {
                    et_now_page.setText(++pageCount + "");
                }
                getOrderListDatas();
                break;
            case R.id.bt_sure:
                pageCount = Integer.parseInt(et_now_page.getText().toString().trim());
                getOrderListDatas();
                break;
            case R.id.bt_first:
                pageCount = 1;
                et_now_page.setText(pageCount + "");
                getOrderListDatas();
                break;
            case R.id.bt_last:
                pageCount = totalPageCount == 0 ? 0 : 1;
                et_now_page.setText(pageCount + "");
                getOrderListDatas();
                break;
        }
    }




    /**
     * 选择查询起始时间
     */
    private void showStartDialog() {
        if (startDialog == null) {
            startDialog = new CalendarDialog(mContext, "起始时间");
            startDialog.setiDialogListener(new CalendarDialog.IDialogListener() {
                @Override
                public void getTimeMillions(long timeMillions) {
//                    startTimes = timeMillions;
//                    tv_start_time.setText(DateUtils.formatDateTimeYMD(startTimes));
//                    showEndDialog();
                }

                @Override
                public void onDismiss(Calendar calendar) {
                    startTimes = calendar.getTimeInMillis();
                    tv_start_time.setText(DateUtils.formatDateTimeYMD(startTimes));
                    pageCount = 1;

                    getOrderListDatas();


                    showEndDialog();
                }
            });
        }
        startDialog.setChooseDay(startTimes);
        startDialog.setMaxChooseDay(Calendar.getInstance().getTimeInMillis());
        startDialog.show();
    }

    /**
     * 选择查询结束时间
     */
    private void showEndDialog() {
        if (endDialog == null) {
            endDialog = new CalendarDialog(mContext, "结束时间");
            endDialog.setChooseDay(endTimes);
            endDialog.setiDialogListener(new CalendarDialog.IDialogListener() {
                @Override
                public void getTimeMillions(long timeMillions) {
//                    endTimes = timeMillions;
//                    tv_end_time.setText(DateUtils.formatDateTimeYMD(endTimes));
                }

                @Override
                public void onDismiss(Calendar calendar) {
                    endTimes = calendar.getTimeInMillis();
                    tv_end_time.setText(DateUtils.formatDateTimeYMD(endTimes));
                    pageCount = 1;

                    getOrderListDatas();

                }
            });
        }
        if (endTimes < startTimes) {
            endDialog.setChooseDay(startTimes);
        } else {
            endDialog.setChooseDay(endTimes);
        }

        endDialog.setMinChooseDay(startTimes);
        endDialog.show();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.rb_all_time://所有时间段
                //查询所有时间段,指定查询订单类型
                ll_calendar.setVisibility(View.GONE);
                Calendar calendar = Calendar.getInstance();
                calendar.set(2000, 1, 1);
                startTimes = DateUtils.getDayLastSecond(calendar);
                endTimes = DateUtils.getDayLastSecond(Calendar.getInstance());
                break;
            case R.id.rb_current_day:
                //查询当前日,指定查询订单类型
                ll_calendar.setVisibility(View.GONE);
                startTimes = DateUtils.getDayFirstSecond(Calendar.getInstance());
                endTimes = DateUtils.getDayLastSecond(Calendar.getInstance());
                break;
            case R.id.rb_last_week:
                //查询最近一周,指定查询订单类型
                ll_calendar.setVisibility(View.GONE);
                long lastWeekNow = DateUtils.getFirstWeekFirstSecond();
                Calendar temCal = Calendar.getInstance();
                temCal.setTimeInMillis(lastWeekNow);
                startTimes = DateUtils.getDayFirstSecond(temCal);
                endTimes = DateUtils.getDayLastSecond(Calendar.getInstance());
                break;
            case R.id.rb_other:
                ll_calendar.setVisibility(View.VISIBLE);
//                tv_start_time.setText(DateUtils.formatDateTimeYMD(startTimes));
//                tv_end_time.setText(DateUtils.formatDateTimeYMD(endTimes));
                break;
            case R.id.rb_all_state:
                //查询所有加油状态的 固定时间日期内
                isOrdered = Common.ORDER_TYPE_ALL;
                break;
            case R.id.rb_order:
                //已预约的 固定时间日期内
                isOrdered = Common.ORDER_TYPE_ORDERED;
                break;
            case R.id.rb_normal:
                //未预约的 固定时间日期内
                isOrdered = Common.ORDER_TYPE_NOORDERED;
                break;
        }
        pageCount = 1;

        getOrderListDatas();
    }

    @Override
    public void onClickItem(int position, ExceptionOrderModle bean) {
        Intent intent = new Intent();
        intent.putExtra("bean", bean);
        INextFragment.replaceNext(intent, ExcOrderDetailFragment.class.getName());
    }

    private void insertTestDatas(){
        for (int i = 0; i < 100; i++) {
            ExceptionOrderModle mod1 = new ExceptionOrderModle();
            mod1.setPayMode("1");
            mod1.setMachineId("2");
            mod1.setUsePrice("1" + i+".0");
            mod1.setCreateD(System.currentTimeMillis());
            mod1.setCuPrice("2");
            mod1.setCustomName("哈哈"+i);
            mod1.setAmount("222");
            mod1.setDiscountPrice("1");
            mod1.setFuelMoney(""+i);
            mod1.setIsError(false);
            mod1.setMachineFlowNum("22");
            mod1.setMlAmount("1");
            mod1.setOilCardId("11");
            mod1.setOilGunId("23");
            mod1.setOilPotId("32");
            mod1.setOrderNum("23232");
            mod1.setUploadState("1");
            mod1.setToken("23");
            mod1.setPayState("1");
            mod1.setPayMode("1");
            ExceptionOrderDao.getInstance().testAddModle(mod1);
        }
    }

    public void getOrderListDatas() {
        excDatas.clear();
        excDatas.addAll(ExceptionOrderDao.getInstance()
                .queryAllDescWithPage(startTimes, endTimes, isOrdered, pageCount, contentCount));
        excOrderListAdapter.notifyDataSetChanged();
    }
}
